package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo11 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("修改用户信息");
        System.out.print("请输入用户名:");
        String username = scan.next();
        System.out.print("请输入密码:");
        String password = scan.next();
        System.out.print("请输入新密码:");
        String new_password = scan.next();
        System.out.print("请输入新昵称:");
        String new_nickname = scan.next();

        try ( Connection connection = DBUtil.getConnection();){
           String sql = "UPDATE userinfo " +
                   "SET password = ? , nickname = ? " +
                   "WHERE username = ? AND password = ?";

            PreparedStatement st = connection.prepareStatement(sql);
            st.setString(1, new_password);
            st.setString(2, new_nickname);
            st.setString(3, username);
            st.setString(4, password);
            int count = st.executeUpdate();
            System.out.println(count > 0 ? "修改成功" : "修改失败");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
